package me.ree.zkproduct.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import me.ree.zkproduct.dto.PageQueryDTO;
import me.ree.zkproduct.entity.Product;
import me.ree.zkproduct.manager.IProductOrdersManager;
import me.ree.zkproduct.service.IProductService;
import me.ree.zkproduct.util.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author ree
 * @since 2024-05-17
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productServiceImpl;

    @Autowired
    private IProductOrdersManager productOrdersManagerImpl;


    // 创建新的商品
    @PostMapping("/")
    public Resp<String> createProduct(@RequestBody Product product) {
        if (product == null) {
            return Resp.fail("参数对象不能为null");
        }
        if (product.getGmtCreate() == null || product.getGmtCreate().longValue() == 0L) {
            product.setGmtCreate(System.currentTimeMillis());
        }
        if (product.getGmtModified() == null || product.getGmtModified().longValue() == 0L) {
            product.setGmtModified(System.currentTimeMillis());
        }
        product.setProductCode(product.getProductCode()+"-"+System.currentTimeMillis());
        boolean result = productServiceImpl.save(product);
        if (result) {
            return Resp.ok("ok");
        }

        return Resp.fail("新增商品失败");
    }

    // 获取所有商品
    @GetMapping("/")
    public Resp<List<Product>> getAllProducts() {
        List<Product> list = productServiceImpl.list();
        return Resp.ok(list);
    }

    /*
    * // 多个参数示例
    * @GetMapping("/users/{userId}/orders/{orderId}")
    * public String getOrder(@PathVariable String userId, @PathVariable String orderId) {
    *      // 使用userId和orderId参数获取订单信息
    *      return "Order for user " + userId + ": " + orderId;
    * }
    * */
    // 根据 ID 获取商品
    @GetMapping("/id/{id}")
    public Resp<Product> getProductById(@PathVariable Long id) {
        Product product = productServiceImpl.getById(id);
        return Resp.ok(product);
    }

    // 根据 productCode 获取商品
    // 用@PathVariable获取productCode时，url如果是/{productCode}，spring无法与/{id}区分开，所以加了/code和/id
//    @GetMapping("/code/{productCode}")
//    public Resp<Product> getProductByCode(@PathVariable String productCode) {
//        Product product = productServiceImpl.getProductByCode(productCode);
//        return Resp.ok(product);
//    }

    @GetMapping("/code")
    public Resp<Product> getProductByCode(@RequestParam String productCode) {
        Product product = productServiceImpl.getProductByCode(productCode);
        return Resp.ok(product);
    }

    // 分页查询商品，支持多种条件
    @GetMapping("/page")
    public Resp<IPage<Product>> getProductsPage(@RequestBody PageQueryDTO<Product> productPageQueryDTO) {
        IPage<Product> productsPage = productServiceImpl.getProductsPage(productPageQueryDTO);
        return Resp.ok(productsPage);
    }

    // 更新商品
    @PutMapping("/")
    public Resp<Boolean> updateProduct(@RequestBody Product product) {
        // 更新的时候不能修改创建时间
        if (product.getGmtCreate() != null) {
            product.setGmtCreate(null);
        }
        // 更新时间为空时，自动赋值
        if (product.getGmtModified() == null || product.getGmtModified().longValue() == 0L) {
            product.setGmtModified(System.currentTimeMillis());
        }
        // 唯一索引不应该被修改
        if (product.getProductCode() != null) {
            product.setProductCode(null);
        }
        boolean result = productServiceImpl.updateById(product);
        return Resp.ok(result);
    }

    // 删除商品
    @DeleteMapping("/{id}")
    public Resp<Boolean> deleteProduct(@PathVariable Long id) {
        boolean result = productServiceImpl.removeById(id);
        return Resp.ok(result);
    }

    @PutMapping("/reduce/stock/{id}")
    public Resp<String> reduceStock(@PathVariable Long id) {
        return productOrdersManagerImpl.reduceStock(id);
    }
}
